<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>	
<%@ include file="/pub/headmeta.jsp" %>
</head>
<body>
	<div class="right-head"><span class="right-head-font">商品排序</span></div>
	
	<form id="queryForm" method="post">
	<div class="term">
		<div class="div1-2">商品大分类：</div>
		<div class="div2" id="typeArea"></div>
	</div>
	<div class="function-bar">
		<div class="div0"><input  type="button" class="button-bar" value="新增" onclick="viewAdd();" ${f:op(ops,'商品排序','新增')}/></div>
		<div class="div0"><input  type="button" class="button-bar" value="删除" onclick="doDel();"  ${f:op(ops,'商品排序','删除')}/></div>		
	</div>	
	</form>
					
	<form id="delForm" method="post">
	<table id="MyTableList" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="table-form">
		<tr align="center" class="STYLE1">
			<th width="5%"><input id="ckAll" type="checkbox" onclick="checkAll(this.checked,'cb');" /></th>
			<th width="5%">序号</th>
			<th width="15%">商品名称</th>
			<th width="25%">建议</th>				
			<th width="15%">图片</th>
			<th width="20%">商品类别</th>				
			<th width="15%">操作</th>
		</tr>
	</table>
	<%@ include file="/pub/pageLink.jsp" %>
	</form>	
</body>
</html>
<script type="text/javascript">
<!--
//自动调用初始化
$(document).ready(function(){	
	//获得类型
	var url="${ctx}/goods!getTopType.action";
	$.ajax({type : "post",   
        url : url,   
        data : '',   
        async : false,   
        success : function callback(txt){   
        	var json = $.parseJSON(txt);
    		if (json.code==0) {	
    			$("#typeArea").html(createTypeRd(json.info,20,'type','${type}'));
    		}else{
    			alert('查询分类失败！');
    		}
		  }	
	});
	//查询
	doQuery();
});

//获取分类多选的radio Html字符串
function createTypeRd(objs,colNum,name,defaultType){
	var s="";
	for(var i=0;i<objs.length;i+=colNum){		
		for(var j=0;j<colNum;j++){
			if((i+j)<objs.length){
				var o=objs[i+j];
				var c="";
				if(defaultType==''){
					c=(i+j)==0?"checked='checked'":"";
				}else{
					c=defaultType==o.goodsTypeId?"checked='checked'":"";
				}
				s+="<input type='radio' name='"+name+"' value='"+o.goodsTypeId+"' "+c+" onclick='doQuery();'/>"+o.name+"&nbsp;&nbsp;&nbsp;";
			}			
		}
		s+="<br/>";
	}
	return s;
}
var listData;//记录当前页数据
//查询
function doQuery(){	
	var page=getPage();
	var pageSize=getPageSize();
	var url="${ctx}/goods!getTypeList.action?page="+page+"&pageSize="+pageSize;
	$.post(url, $("#queryForm").serialize() ,function callback(txt){  
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				setRecordCount(json.count);
				listData=json.info;
				createTable(listData,page,pageSize);				
			}else{
				alert('查询失败！');
			}
		}	
	);
	$('#ckAll').attr("checked",false);
}

//删除
function doDel(){
	if(ifCheck('cb')){
		if(confirm('确定要从类型中删除选中的记录吗？')){
			var url="${ctx}/goods!removeOrder.action?type="+$("input[name='type']:checked").val();
			$.post(url, $("#delForm").serialize() ,function callback(txt){   
					var json = $.parseJSON(txt);
					if (json.code==0) {	
						doQuery();			
					}else{
						alert('删除失败！');
					}
				}	
			);			
		}
	}else{
		alert('请先勾选记录！');
	}
}

//上移
function moveUp(pos){
	if(pos>0){
		exchangePos(listData[pos-1],listData[pos]);			
	}else if(pos==0&&getPage()>1){//当前页第一条且页面非第一页
		var p=(getPage()-1)*getPageSize();		
		var url="${ctx}/goods!getTypeList.action?page="+p+"&pageSize=1&type="+$("input[name='type']:checked").val();
		$.ajax({type : "post",   
	          url : url,   
	          data : $("#queryForm").serialize(),   
	          async : false,   
	          success : function callback(txt){   
				var json = $.parseJSON(txt);
				if (json.code==0) {
					exchangePos(json.info[0],listData[pos]);							
				}else{
					alert('查询失败！');
				}
			  }	
		});
	}else{
		alert('已经在最首位置！');
	}
}

//下移
function moveDown(pos){
	if(pos<listData.length-1){
		exchangePos(listData[pos],listData[pos+1]);		
	}else if(pos==listData.length-1&&getPage()<getAllPage()){//当前页最后一条且页面非最后一页
		var p=getPage()*getPageSize()+1;		
		var url="${ctx}/goods!getTypeList.action?page="+p+"&pageSize=1&type="+$("input[name='type']:checked").val();
		$.ajax({type : "post",   
	          url : url,   
	          data : $("#queryForm").serialize(),   
	          async : false,   
	          success : function callback(txt){   
				var json = $.parseJSON(txt);
				if (json.code==0) {
					exchangePos(listData[pos],json.info[0]);							
				}else{
					alert('查询失败！');
				}
			  }	
		});
	}else{
		alert('已经在最末位置！');
	}	
}

//交换位置
function exchangePos(obj1,obj2){
	var url="${ctx}/goods!exchangePos.action";
	$.post(url, "id1="+obj1.goodsId+"&id2="+obj2.goodsId+"&type="+$("input[name='type']:checked").val() ,function callback(txt){   
			var json = $.parseJSON(txt);
			if (json.code==0) {			
				doQuery();			
			}else{
				alert('交换位置失败！');
			}
		}	
	);	
}

//移至首位
function moveFirst(pos){
	var url="${ctx}/goods!moveFirst.action";
	$.post(url, "id="+listData[pos].goodsId+"&type="+$("input[name='type']:checked").val() ,function callback(txt){   
			var json = $.parseJSON(txt);
			if (json.code==0) {	
				doQuery();			
			}else{
				alert('移至首位失败！');
			}
		}	
	);
}

//进入新增页面
function viewAdd(){
	location.href="${ctx}/goods!viewLeftList.action?type="+$("input[name='type']:checked").val();
}

//创建表数据
function createTable(objs,page,pageSize){	
	var table=document.getElementById('MyTableList');
	for( var i=table.rows.length-1; i>0; i--){
		table.deleteRow(i); 
	}
	var b=document.createElement("tbody");
	for(var i=0;i<objs.length;i++){
		var obj=objs[i];
		var row=document.createElement("tr");	
		row.align="center";	
		
		var c1=document.createElement("td");	
		c1.innerHTML="<input name='cb' type='checkbox' value='"+obj.goodsId+"'/>";
		var c2=document.createElement("td");	
		c2.innerHTML=(page-1)*pageSize+i+1;
		var c3=document.createElement("td");	
		c3.innerHTML="<a class='edit-font' href='javascript:openWin(\""+obj.link+"\",800,600);'>"+obj.title+"</a>";	
		var c4=document.createElement("td");	
		c4.innerHTML=obj.suggest;
		var c5=document.createElement("td");	
		c5.innerHTML="<img height='40' width='40' src='${ctx}/goods!getSou.action?picName="+obj.pic+"'/>";
		var c6=document.createElement("td");	
		c6.innerHTML=obj.typeName;		
		var c7=document.createElement("td");
		c7.innerHTML="<span ${f:op(ops,'商品排序','位置变换')}><a class='edit-font' href=\"javascript:moveUp("+i+");\">上移</a>&nbsp;<a class='edit-font' href=\"javascript:moveDown("+i+");\">下移</a>&nbsp;<a class='edit-font' href=\"javascript:moveFirst("+i+");\">移至首位</a></span>";
	
		row.appendChild(c1);
		row.appendChild(c2);
		row.appendChild(c3);
		row.appendChild(c4);
		row.appendChild(c5);
		row.appendChild(c6);
		row.appendChild(c7);

		setTdStyle(row);
		b.appendChild(row);
	}
	table.appendChild(b);
}

//-->
</script>
